package com.huawei.petapp2.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.huawei.petapp2.entity.User;

import java.util.HashSet;
import java.util.Set;

public class PreferenceHelper {
    private static String IDENTIFY = "com.deeepthinking";

     /*
        SharedPreferences只能存储简单的数据，不能存储复杂的对象；
        使用SharedPreferences可以方便存储一些简单的数据，比如设置，密码等；
        使用SharedPreferences保存需要用到Editor；
        获取数据的时候可以指定默认值
    */

    /*存储String
    *需要传入key和value，每个key对应一个value；需要传入系统的上下文环境Context；
    * 保存的时候，需要使用SharedPreferences的Editor；保存完需要调用commit完成保存；
    * 获取数据需要key获取相应的value；获取的时候，可以指定默认值，当没有值的时候，返回默认值；
    */
    public static void putString(String key, String value, Context context) {
        SharedPreferences sharedPref = context.getSharedPreferences(IDENTIFY, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public static String getString(String key, Context context) {
        SharedPreferences sharedPref = context.getSharedPreferences(IDENTIFY, Context.MODE_PRIVATE);
        return sharedPref.getString(key, "");
    }

    public static void saveLoginInfo(Context context, User user,String Msg){
       /* Result result =DateUtil.selectList(
                HttpAddress.get(HttpAddress.user(),"login",password,username));
        System.out.println(result.getResult());
        User users=DateUtil.getEntity(result,User.class);*/
      //  User user=new User(username,password);
        //获取SharedPreferences对象
        SharedPreferences sharedPre=context.getSharedPreferences( IDENTIFY , context.MODE_PRIVATE);
        //获取Editor对象
        SharedPreferences.Editor editor=sharedPre.edit();
        //设置参数
        editor.putString( "token" ,Msg);
        editor.putInt("userId",user.getUserId());
        editor.putString( "username" ,user.getUsername());
        editor.putString( "password" ,user.getPassword());
        editor.putString("img",user.getUserImg());
        editor.putFloat("balance",user.getUserfound());
        editor.putInt("userid",user.getUserId());
        editor.putString("telephone",user.getUserEmail());
        //提交
        editor.apply();
    }

    //当前用户的存储购物车的id
    public static void saveCartid(Context context, int cartid){
        SharedPreferences sharedPre=context.getSharedPreferences( IDENTIFY , context.MODE_PRIVATE);
        //获取Editor对象
        SharedPreferences.Editor editor=sharedPre.edit();
        editor.putString( "cartId" , String.valueOf(cartid));
        //提交
        editor.apply();
    }


    //当前用户的存储用户的的默认地址
    public static void saveDefalue(Context context, String addrId,String receiverName,String receiverMobile,String addr,
                                   String province,String city,String area){
        SharedPreferences sharedPre=context.getSharedPreferences( IDENTIFY , context.MODE_PRIVATE);
        //获取Editor对象
        SharedPreferences.Editor editor=sharedPre.edit();
        editor.putString( "addrId" ,addrId);
        editor.putString("receiverName",receiverName);
        editor.putString("receiverMobile",receiverMobile);
        editor.putString("addr",addr);
        editor.putString("province",province);
        editor.putString("city",city);
        editor.putString("area",area);
        //提交
        editor.apply();
    }


    /*
     *存储Set
     */
    public static void putSet(String key, Set<String> values, Context context) {
        SharedPreferences sharedPref = context.getSharedPreferences(IDENTIFY, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putStringSet(key, values);
        editor.commit();
    }

    public static Set<String> getSet(String key, Context context) {
        SharedPreferences sharedPref = context.getSharedPreferences(IDENTIFY, Context.MODE_PRIVATE);
        return sharedPref.getStringSet(key, new HashSet<String>());
    }

    /*存储int
    **/
    public static void putInt(String key, int value, Context context) {
        SharedPreferences sharedPref = context.getSharedPreferences(IDENTIFY, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public static int getInt(String key, Context context) {
        SharedPreferences sharedPref = context.getSharedPreferences(IDENTIFY, Context.MODE_PRIVATE);
        return sharedPref.getInt(key, 0);
    }

    public static Float getFloat(String key, Context context) {
        SharedPreferences sharedPref = context.getSharedPreferences(IDENTIFY, Context.MODE_PRIVATE);
        return sharedPref.getFloat(key, 0);
    }
    /*
    删除指定的数据
    */
    public static void removeData(String key, Context context) {
        SharedPreferences sharedPref = context.getSharedPreferences(IDENTIFY, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.remove(key);
        editor.commit();
    }

}
